/**
 * Copyright(c)2012 Beijing PeaceMap Co.,Ltd.
 * All right reserved. 
 */
package com.pmc.dwa.security.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.pmc.dwa.common.dao.IGenericDao;
import com.pmc.dwa.common.utils.PaginationSupport;
import com.pmc.dwa.security.model.TRole;
import com.pmc.dwa.security.model.TSysoperLog;
import com.pmc.dwa.security.service.IRoleLogService;

/**
 * @description 角色／日志管理
 * @author aokunsang
 * @date 2013-1-9
 */
@Service("rolelogService")
public class RoleLogServiceImpl implements IRoleLogService {
	
	@Resource(name="genericDao")
	private IGenericDao genericDao;
	
	final static String clearOperLog = "delete from t_sysoper_log";
	final static String getOperLog = "select * from t_sysoper_log where ID = ?";
	final static String delOperLogById = "delete from t_sysoper_log where ID = ?";
	final static String addRole = "insert into t_role(PID,VERSION,ROLENAME,COMMENTS,STATUS,CREATE_TIME,UPDATE_TIME) values(:pid,:version,:rolename,:comments,:status,:create_time,:update_time)";
	final static String editRole = "update t_role set PID=:pid,ROLENAME=:rolename,COMMENTS=:comments,UPDATE_TIME=:update_time where ID=:id";
	final static String getAllSectForRole = "select a.*,b.SECT_ID from t_sect a left join(select sect_id from t_role_sect where role_id = ?) b on a.id=b.sect_id order by a.group_id asc, a.slevel asc, a.sort asc, a.updatetime desc";
	final static String addSectInRole = "insert into t_role_sect(ROLE_ID,SECT_ID) values (?,?)";
	final static String chkRoleSect = "select count(ID) from t_role_sect rs where rs.ROLE_ID = ? and rs.SECT_ID = ?";
	
	@Override
	public PaginationSupport<TSysoperLog> getAllOperLog(String strSql,
			int pageNumber, int pageSize,Date bdate,Date edate) {
		String queryString = "select * from t_sysoper_log where 1=1 "+(strSql.length()>0?strSql:"");
		return genericDao.queryListByRows(queryString + " order by OPER_TIME desc", TSysoperLog.class, pageNumber, pageSize,bdate!=null&&edate!=null?new Object[]{bdate,edate}:new Object[]{});
	}

	@Override
	public TSysoperLog getOperLog(String logid) {
		return genericDao.find(getOperLog, TSysoperLog.class, logid);
	}

	@Override
	public void delAllOperLog() {
		genericDao.update(clearOperLog);
	}

	@Override
	public void delOperLogById(String logid) {
		genericDao.update(delOperLogById,logid);
	}

	@Override
	public PaginationSupport<TRole> getPagesTRole(String comments,
			int pageNumber, int pageSize) {
		String queryString = "select * from t_role where 1=1 ";
		if(StringUtils.isNotEmpty(comments)){
			queryString += " and COMMENTS like '%"+comments+"%'";
		}
		return genericDao.queryListByRows(queryString + " order by UPDATE_TIME desc", TRole.class, pageNumber, pageSize);
	}

	@Override
	public TRole getRole(String roleid) {
		return genericDao.find("select * from t_role where ID = ?", TRole.class, roleid);
	}

	@Override
	public void delRole(String roleid) {
		genericDao.update("delete from t_role_sect where ROLE_ID = ?", roleid);
		genericDao.update("delete from t_role where ID = ?", roleid);
	}

	@Override
	public void addRole(TRole role) {
		genericDao.saveOrUpdate(addRole, role);
	}

	@Override
	public void editRole(TRole role) {
		genericDao.saveOrUpdate(editRole, role);
	}

	@Override
	public List<Map<String, Object>> getAllSectForRole(String roleid) {
		return genericDao.queryMapList(getAllSectForRole, roleid);
	}

	@Override
	public boolean chkRoleSect(String roleid, String sectid) {
		long statCount = genericDao.stat(chkRoleSect,roleid,sectid);
		return statCount>0?true:false;
	}

	@Override
	public void addSectInRole(String roleid, String sectid) {
		genericDao.update(addSectInRole, new Object[]{roleid,sectid});
	}

	@Override
	public void delRoleSectById(String roleid, String sectid) {
		genericDao.update("delete from t_role_sect where ROLE_ID = ? and SECT_ID = ?", roleid, sectid);
	}
}
